package com.shxc.smarttrade.index.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.index.model.ShowImportanceArticleModel;
import com.shxc.smarttrade.utils.AppUtils;
import com.shxc.smarttrade.utils.GlideUtils;
import com.shxc.smarttrade.utils.Loger;

import org.xchris.view.annotation.ViewInject;
import org.xchris.x;

import java.util.List;

/**
 * 首页 - 新闻
 */
public class IndexNewsAdapter extends BaseAdapter {
    private Context c;
    private List<ShowImportanceArticleModel.DataBean> list;
    private Handler handler;
    private GlideUtils glideUtils;

    public IndexNewsAdapter(Context c, List<ShowImportanceArticleModel.DataBean> list, Handler handler) {
        this.c = c;
        this.handler = handler;
        this.list = list;
        glideUtils = new GlideUtils(c);
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoledr holedr = null;
        if (convertView == null) {
            convertView = View.inflate(c, R.layout.item_index_news, null);
            holedr = new ViewHoledr();
            x.view().inject(holedr, convertView);//ViewHolder的注释
            convertView.setTag(holedr);
        } else {
            holedr = (ViewHoledr) convertView.getTag();
        }

        final ShowImportanceArticleModel.DataBean model = list.get(position);

        holedr.title_textview.setText(model.getTitle());
        holedr.content_textview.setText(model.getIntroduce());

        if (AppUtils.isEmpty(model.getImgURL())) {
            holedr.imageview.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holedr.content_textview.getLayoutParams();
            layoutParams.setMargins(100, 20, 10, 5);//4个参数按顺序分别是左上右下
        } else {
            Loger.e("url", model.getImgURL());
            holedr.imageview.setVisibility(View.VISIBLE);
            glideUtils.showFillet(model.getImgURL(), holedr.imageview, 5,R.mipmap.no_order,R.mipmap.no_order);
        }
        holedr.sourse_textview.setText("来源：SmartTrade");
        holedr.time_textview.setText(model.getTime());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 0;
                msg.obj = model;
                handler.sendMessage(msg);
            }
        });
        return convertView;
    }

    public void setList(List<ShowImportanceArticleModel.DataBean> list) {
        this.list.clear();
        for (int i = 0; i < list.size(); i++) {
            this.list.add(list.get(i));
        }
        notifyDataSetChanged();
    }

    public void upDate(List<ShowImportanceArticleModel.DataBean> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    static class ViewHoledr {

        @ViewInject(R.id.title_textview)
        TextView title_textview;
        @ViewInject(R.id.content_textview)
        TextView content_textview;
        @ViewInject(R.id.imageview)
        ImageView imageview;
        @ViewInject(R.id.sourse_textview)
        TextView sourse_textview;
        @ViewInject(R.id.time_textview)
        TextView time_textview;
    }
}
